<?php

namespace app\stock\controller;

use think\Controller;

class Source extends Controller
{
    // 客户来源模型
    protected $SourceModel;

    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->SourceModel = model('Business.Source');
    }
    /**
     * 添加
     */
    public function add()
    {
        $name = $this->request->param('name', '', 'trim');
        if (!$name) {
            $this->error("参数不能为空");
        }
        $result = $this->SourceModel->validate('common/Business/Source')->save(['name' => $name]);
        if (!$result) {
            $this->error($this->SourceModel->getError());
        }
        $this->success("添加成功");
    }
    /**
     * 修改
     */
    public function edit()
    {
        $id = $this->request->param('id', 0, 'trim');
        $name = $this->request->param('name', '', 'trim');
        if (!$name) {
            $this->error("参数不能为空");
        }
        $source=$this->SourceModel->find($id);
        if (!$source) {
            $this->error("来源不存在");
        }
        $result = $this->SourceModel->validate('common/Business/Source')->isUpdate()->save(['name' => $name], ['id' => $id]);
        if (!$result) {
            $this->error($this->SourceModel->getError());
        }
        $this->success("修改成功");
    }
    /**
     * 删除
     */
    public function del()
    {
        $id = $this->request->param('id', 0, 'trim');
        $source=$this->SourceModel->find($id);
        if (!$source) {
            $this->error("来源不存在");
        }
        $result = $this->SourceModel->destroy($id);
        if (!$result) {
            $this->error("删除失败！");
        }
        $this->success("删除成功");
    }
    /**
     * 列表
     */
    public function index(){
        $list = $this->SourceModel->order('id','desc')->select();
        $this->success("","",$list);
    }
    /**
     * 详情
     */
    public function info(){
        $id = $this->request->param('id', 0, 'trim');
        $source=$this->SourceModel->find($id);
        if (!$source) {
            $this->error("来源不存在");
        }
        $this->success("","",$source);
    }

}
